/*
 * This file is part of JMesh.
 *
 * last modified: September 20, 10:00 AM
 *
 */
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.element.force.ElementForce;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.dialogs.SurfaceForcesDialog;

/**
 *
 * @author Pitangueira, Roque & Brugiolo, Marco  
 * @version 0.0.1 Beta 
 */
public class AddSurfaceForceCommand implements Command, Cloneable {

	private InsaneInternalInterface parent;
	private Controller controller;
	private DrawingArea da;

	/** Creates new AddSurfaceForceCommand */
	public AddSurfaceForceCommand(JInternalFrame parent) {
		this.parent = (InsaneInternalInterface)parent;
		controller = this.parent.getController();
		da = this.parent.getDrawing();
	}

	public void execute() {
		ArrayList els = controller.getSelection();
		if ( els.size() < 1 ) {
			controller.selectAll();
			els = controller.getSelection();
		}

		ListIterator elements = els.listIterator();
		while(elements.hasNext()) {
			ParametricElement element = (ParametricElement)elements.next();
			SurfaceForcesDialog dlg = new SurfaceForcesDialog(parent, element);
			int retVal = dlg.showDialog();
			if (retVal == TabbedDialog.OK_OPTION){
				int nRow = element.getIncidence().size();
				if ( element.getType(1)=='L' ) {
					if ( element.getType(0)=='T' ) {
						nRow = (int)(((Math.sqrt(1+8*nRow)-1)/2)-1)*3;
					} else {
						nRow = (int)(Math.sqrt(nRow)-1)*4;
					}
				}
				String[][] sForces = dlg.getSurfaceForce();
				int lb = 0;
				int lb2 = 0;
				int last = 0;
				for (int count = (nRow - 1); count >= 0; count--) {
					double Fxi = 0;
					double Fyi = 0;
					double Mzi = 0;
					double Fxf = 0;
					double Fyf = 0;
					double Mzf = 0;
					if ( sForces[count][1] != null && sForces[count][1] != "" )
						Fxi = Double.parseDouble( sForces[count][1] );
					if ( sForces[count][2] != null && sForces[count][2] != "" )
						Fyi = Double.parseDouble( sForces[count][2] );
					if ( sForces[count][3] != null && sForces[count][3] != "" )
						Mzi = Double.parseDouble( sForces[count][3] );
					if ( sForces[count][4] != null && sForces[count][4] != "" )
						Fxf = Double.parseDouble( sForces[count][4] );
					if ( sForces[count][5] != null && sForces[count][5] != "" )
						Fyf = Double.parseDouble( sForces[count][5] );
					if ( sForces[count][6] != null && sForces[count][6] != "" )
						Mzf = Double.parseDouble( sForces[count][6] );
					if ( Fxi!=0 || Fyi!=0 || Mzi!=0 || Fxf!=0 || Fyf!=0 || Mzf!=0 ) {
						lb++;
						Node n1 = (Node)element.getIncidence().get(count);
						Node n2 = (Node)element.getIncidence().get(last);
						PointForce p1 = new PointForce();
						p1.setLabel("" + element.getLabel() + ".pf" + lb );
						p1.setCoord( n1.getCoord() );
						p1.setForce(0, Fxi);
						p1.setForce(1, Fyi);
						p1.setForce(5, Mzi);
						PointForce p2 = new PointForce();
						lb++;
						p2.setLabel("" + element.getLabel() + ".pf" + lb);
						p2.setCoord( n2.getCoord() );
						p2.setForce(0, Fxf);
						p2.setForce(1, Fyf);
						p2.setForce(5, Mzf);
						ElementForce e1 = new ElementForce();
						lb2++;
						e1.setLabel("" + element.getLabel() + ".ef" + lb2);
						//e1.addPointForce(p1);
						//e1.addPointForce(p2);
						//element.addSurfaceForce(e1);
					}
					last = count;
				}
				parent.setMessage("Adding Surface Force");
			}
		}

		parent.nothingToUndo();
		parent.nothingToRedo();
		controller.unSelectAll();
		//controller.setStatus();
		controller.compose();
		da.repaint();
	}

	public void undo() {
	}

	public void redo() {
	}

	public Object clone() {
		try {
			AddSurfaceForceCommand cmd = (AddSurfaceForceCommand)super.clone();
			return cmd;
		}catch (CloneNotSupportedException e) {
			throw new Error("Clonning ERROR - This should never happen!");
		}
	}
}
